package com.example.yjhero.safeapp;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLOutput;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    private Context context;

    //所有成员变量弄成私有，控制读写权限，用get,set
    private ProgressDialog mprogressDialog;

    //protect只能给它的子类用

    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.example.yjhero.safeapp", appContext.getPackageName());
    }

    @Test
    public void test01(){
        PackageManager manager=context.getPackageManager();
        try {
            PackageInfo packageInfo=manager.getPackageInfo(context.getPackageName(),0);
            int versionCode=packageInfo.versionCode;
            String versionName=packageInfo.versionName;
            System.out.println(versionCode);
            System.out.println(versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testNet(){
        //状态码





        try {
            URL url=new URL("http://10.0.0.0:8080/update.html");
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setRequestMethod("GET");

            InputStream in=connection.getInputStream();
            BufferedReader reader=new BufferedReader(new InputStreamReader(in));
            StringBuilder resqonse=new StringBuilder();

            String line;
            while ((line=reader.readLine())!=null){
                resqonse.append(line);
            }
            String result=resqonse.toString();
            JSONObject json=new JSONObject(result);

            String code=json.getString("code");
            String des =json.getString("des");
            String apkurl=json.getString("apkurl");

            //String version=Version

        } catch (MalformedURLException e) {
            //网络问题
            e.printStackTrace();
        } catch (IOException e) {
            //服务器问题
            e.printStackTrace();
        } catch (JSONException e) {
            //json解析
            e.printStackTrace();
        }
    }

}
